package org.flyby.api.daoImpl;

import org.flyby.api.dao.CartDao;
import org.flyby.api.dao.ProductOrderDetailsDao;
import org.flyby.api.persistence.Cart;
import org.flyby.api.persistence.Product;
import org.flyby.api.persistence.ProductOrderDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ProductOrderDetailsDaoImpl extends BaseCommonDaoImpl<ProductOrderDetails> implements ProductOrderDetailsDao {

    @Autowired
    private CartDao cartDao;

    @Override
    public ProductOrderDetails persistProductIntoCart(Cart cart, Product product, int units) {

        ProductOrderDetails productOrderDetails = new ProductOrderDetails();

        productOrderDetails.setCart(cart);
        productOrderDetails.setUnits(units);
        productOrderDetails.setProduct(product);
        productOrderDetails.setOrderPrice(product.getPrice() * units);
        productOrderDetails.setOrderWeight(product.getWeight() * units);
        productOrderDetails = save(productOrderDetails);

        cartDao.addProductOrderDetails(cart, productOrderDetails);

        return productOrderDetails;

    }


}
